package com.example.arithmeticleetcode.leetcode.nov;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-11-17 09:22
 **/
public class Demo10 {

    public static void main(String[] args) {
        int[][] arr1 = {{1,0},{0,1}};
        int[][] arr5 = {{1},{1}};
        int[][] arr6 = {{1},{0}};
        int[][] arr2 = {{1,0},{1,1}};
        int[][] arr3 = {{1,1,0,0},{0,0,1,0},{0,0,1,0},{0,0,0,1}};
        int[][] arr4 = {{1,1,0,1},{0,0,1,0},{0,0,1,0},{0,0,0,1}};
        System.out.println(countServers(arr1));
        System.out.println(countServers(arr2));
        System.out.println(countServers(arr3));
        System.out.println(countServers(arr4));
        System.out.println(countServers(arr5));
        System.out.println(countServers(arr6));
    }


    public static int countServers(int[][] grid) {
        int count = 0;
        for (int i = 0, len = grid.length; i < len; i++) {
            for (int j = 0, len2 = grid[i].length; j < len2; j++) {
                if (grid[i][j] == 1) {
                    boolean flag = true;
                    for (int innerI = 0; innerI < len; innerI++) {
                        if (grid[innerI][j] == 1 && innerI != i) {
                            count++;
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        for (int innerJ = 0; innerJ < len2; innerJ++) {
                            if (grid[i][innerJ] == 1 && innerJ != j) {
                                count++;
                                break;
                            }
                        }
                    }
                }
            }
        }
        return count;
    }
}
